<?php function open_image ($file) { 
    # JPEG: 
    $im = @imagecreatefromjpeg($file); 
    if ($im !== false) { return $im; } 
    # GIF: 
    $im = @imagecreatefromgif($file); 
    if ($im !== false) { return $im; } 
    # PNG: 
    $im = @imagecreatefrompng($file); 
    if ($im !== false) { return $im; } 
    # GD File: 
    $im = @imagecreatefromgd($file); 
    if ($im !== false) { return $im; } 
    # GD2 File: 
    $im = @imagecreatefromgd2($file); 
    if ($im !== false) { return $im; } 
    # WBMP: 
    $im = @imagecreatefromwbmp($file); 
    if ($im !== false) { return $im; } 
    # XBM: 
    $im = @imagecreatefromxbm($file); 
    if ($im !== false) { return $im; } 
    # XPM: 
    $im = @imagecreatefromxpm($file); 
    if ($im !== false) { return $im; } 
    # Try and load from string: 
    $im = @imagecreatefromstring(file_get_contents($file)); 
    if ($im !== false) { return $im; } 
    return false; 
} 
	$imageName = $_GET["image_name"];
    $newheight = $_GET["h"]; 
    $newwidth = $_GET["w"]; 
	$thumbWidth = $_GET["w"];
    $imagepath = $imageName; 
    $image = open_image($imagepath); 
    $width = imagesx($image); 
    $height = imagesy($image);
	if($width >= $height) //if original width greter then or equal to original height then
	{ 
		if($width >= $newwidth) //if original width greter then or equal to thumbnail width then
		{
			$new_height = floor( $height * ( $thumbWidth / $width ) );
    		$new_height = $new_height;
			$new_width = $newwidth;  
		}
		else //if original width less then or equal to thumbnail width then
		{
			$thumbWidth = $width;
			$new_height = floor( $height * ( $thumbWidth / $width ) );
    		$new_height = $new_height;
			$new_width = $width;
		}
	}
	else //if original hieght greter then or equal to original width then
	{
		if($height >= $newheight) //if original height greter then or equal to thumbnail height then
		{
			$new_width = floor( $width * ($thumbWidth / $height));
			$new_width = $new_width;
		 	$new_height = $newwidth;
		}
		else //if original height less then or equal to thumbnail height then
		{
			$new_width = floor( $width * ($thumbWidth / $height));
			$new_width = $new_width;
		 	$new_height = $height;
		}
	}
    
    $image_resized = imagecreatetruecolor($new_width, $new_height); 
    imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    header('Content-type: image/jpeg'); 
    imagejpeg($image_resized); 
?>